<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- https://juejin.cn/post/6937469222251560990#heading-11 -->
    <script>
        var result = [];
        var a = 3;
        var total = 0;

        function foo(a) {
            for (var i = 0; i < 3; i++) {
                result[i] = function () {
                    total += i * a;
                    console.log('i',i);
                    console.log('a',a);
                    console.log(total);
                }
            }
        }

        foo(1);
        result[0]();  // 3
        result[1]();  // 6
        result[2]();  // 9

    </script>
</body>

</html>